﻿using System;
using System.Threading;
using System.Windows.Forms;

namespace AsicMonitor
{
    public partial class AddIpDailog : Form
    {
        public AddIpDailog()
        {
            InitializeComponent();

            Win32Utility.SetCueText(ipRange1, "192");
            Win32Utility.SetCueText(ipRange2, "168");
            Win32Utility.SetCueText(ipRange3, "1");

            ipRange1.Text = "192";
            ipRange2.Text = "168";
            ipRange3.Text = "1";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string ip1 = ipRange1.Text;
            string ip2 = ipRange2.Text;
            string ip3 = ipRange3.Text;

            Console.WriteLine(ip1 + " " + ip2 + " " + ip3);

            int p1 = int.Parse(ip1);
            int p2 = int.Parse(ip2);
            int p3 = int.Parse(ip3);

            this.Hide();
            this.OnAddNewIpEvent(p1, p2, p3);
            this.Close();
        }

        public delegate void AddNewIpEvent(int ip1, int ip2, int ip3);
        public event AddNewIpEvent addNewIpEvent;
        protected virtual void OnAddNewIpEvent(int ip1, int ip2, int ip3)
        {
            AddNewIpEvent newEvent = Volatile.Read(ref addNewIpEvent);//保持线程安全,保留一个备份
            if (newEvent != null)
            {
                newEvent(ip1, ip2, ip3);
            }
        }


        public delegate void RemoveNewIpEvent(int ip1, int ip2, int ip3);
        public event RemoveNewIpEvent removeNewIpEvent;
        protected virtual void OnRemoveNewIpEvent(int ip1, int ip2, int ip3)
        {
            RemoveNewIpEvent removeEvent = Volatile.Read(ref removeNewIpEvent);//保持线程安全,保留一个备份
            if (removeEvent != null)
            {
                removeEvent(ip1, ip2, ip3);
            }
        }

        private void removeBtn_Click(object sender, EventArgs e)
        {
            string ip1 = ipRange1.Text;
            string ip2 = ipRange2.Text;
            string ip3 = ipRange3.Text;

            Console.WriteLine(ip1 + " " + ip2 + " " + ip3);

            int p1 = int.Parse(ip1);
            int p2 = int.Parse(ip2);
            int p3 = int.Parse(ip3);

            this.Hide();
            this.OnRemoveNewIpEvent(p1, p2, p3);
            this.Close();
        }
    }
}
